package com.erp.common.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.erp.app.common.ProjectConstants;
import com.erp.app.util.web.WebUtil;

public class SearchMasterAction {
    public static void setSearchVendor(HttpServletRequest request, String status, String exclusive, String functionID,
            String inStatus) {
        setSearchVendor(request, status, exclusive, functionID, inStatus, true);
    }

    public static void setSearchVendor(HttpServletRequest request, String status, String exclusive, String functionID,
            String inStatus, boolean includeInactive) {

        HttpSession session = request.getSession(false);
        LinkedList criteriaList = new LinkedList();
        ArrayList criteriaColumnList = new ArrayList();
        ArrayList rsltTextList = new ArrayList();
        ArrayList rsltColumnList = new ArrayList();
        Hashtable tblParams = new Hashtable();
        HashMap params = new HashMap();
        StringBuffer sb = null;

        criteriaList.add("Vendor Code");
        criteriaList.add("INPUT");
        criteriaList.add("");

        String wh = "";
        wh = "COMPANY_CODE='"
                + WebUtil.getAppropriateCompanyCode(session, WebUtil.getCodeSysParamId(ProjectConstants.CD_COUNTRY)) + "'";
        wh += " AND REF_MASTER_CODE='" + ProjectConstants.CD_COUNTRY + "'";

        criteriaList.add("Vendor Name");
        criteriaList.add("INPUT_WITH_WILDCARD");
        criteriaList.add("");

        criteriaList.add("Country"); // label
        criteriaList.add("COMBO");
        criteriaList.add("");
        criteriaList.add("t_code_dtl"); // table
        criteriaList.add("REF_CODE"); // combo value
        criteriaList.add("REF_DESC"); // combo text
        criteriaList.add(wh); // criteria
        criteriaList.add("REF_DESC"); // order by
        criteriaList.add(""); // submit on change?

        criteriaColumnList.add("A.VENDOR_CODE");
        criteriaColumnList.add("A.VENDOR_NAME");
        criteriaColumnList.add("A.COUNTRY");

        rsltTextList.add("Vendor Code");
        rsltTextList.add("Vendor Name");
        rsltTextList.add("Country");

        rsltColumnList.add("A.VENDOR_CODE");
        rsltColumnList.add("A.VENDOR_NAME");
        rsltColumnList.add("B.REF_DESC");

        String table = " T_VENDOR_HDR A, T_CODE_DTL B";
        StringBuffer whereClause = null;
        if (includeInactive) {
            whereClause = new StringBuffer(" A.STATUS <> '" + ProjectConstants.GENERIC_PROSPECTIVE + "'");
        } else {
            whereClause = new StringBuffer(" A.STATUS = '" + ProjectConstants.GENERIC_ACTIVE + "'");
        }

        whereClause.append(" AND B.COMPANY_CODE(+) = '"
                + WebUtil.getAppropriateCompanyCode(session, WebUtil.getCodeSysParamId(ProjectConstants.CD_COUNTRY)) + "'");
        whereClause.append(" AND B.REF_MASTER_CODE(+) = '" + ProjectConstants.CD_COUNTRY + "'");
        whereClause.append(" AND A.COUNTRY = B.REF_CODE(+)");

        tblParams.put(ProjectConstants.SESSION_TABLE, table);
        tblParams.put(ProjectConstants.SESSION_WHERE, whereClause.toString());
        tblParams.put(ProjectConstants.SESSION_ORDERBY, "A.VENDOR_NAME, A.VENDOR_CODE");

        if (ProjectConstants.GENERIC_YES.equals(exclusive)) {
            params.put(ProjectConstants.SESSION_EXCLUSIVE, exclusive);
        }

        params.put(ProjectConstants.SESSION_SESSION_NAME, inStatus);
        params.put(ProjectConstants.SESSION_SESSION_VALUE, status);
        params.put(ProjectConstants.SESSION_OPENER_PATH, functionID);

        params.put(ProjectConstants.SESSION_CRITERIA_LIST, criteriaList);
        params.put(ProjectConstants.SESSION_CRITERIA_COLUMN_LIST, criteriaColumnList);
        params.put(ProjectConstants.SESSION_RESULT_TEXT_LIST, rsltTextList);
        params.put(ProjectConstants.SESSION_RESULT_COLUMN_LIST, rsltColumnList);
        params.put(ProjectConstants.SESSION_TABLE_PARAMS, tblParams);

        session.setAttribute(ProjectConstants.SESSION_COMMON_SEARCH_PARAMETERS, params);

    }

}